//
//  DiscountCouponCell.m
//  PigMarket
//
//  Created by mac on 2019/5/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "DiscountCouponCell.h"

@interface DiscountCouponCell ()
/**
 bgView
 */
@property (nonatomic , strong) UIView *bgView;
/**
 couponImg
 */
@property (nonatomic , strong) UIImageView *couponImg;
/**
 title
 */
@property (nonatomic , strong) UILabel *title;
/**
 date
 */
@property (nonatomic , strong) UILabel *date;
/**
 useBtn
 */
@property (nonatomic , strong) UIButton *useBtn;
/**
 detailInfoBtn
 */
@property (nonatomic , strong) UIButton *detailInfoBtn;
/**
 detailInfoBgView
 */
@property (nonatomic , strong) UIView *detailInfoBgView;
/**
 detailInfo
 */
@property (nonatomic , strong) UILabel *detailInfo;
/**
 lineImg
 */
@property (nonatomic , strong) UIImageView *lineImg;

@end
@implementation DiscountCouponCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    DiscountCouponCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[DiscountCouponCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.couponImg];
    [self.bgView addSubview:self.title];
    [self.bgView addSubview:self.useBtn];
    [self.bgView addSubview:self.date];
    [self.bgView addSubview:self.lineImg];
    [self.bgView addSubview:self.detailInfoBtn];
    [self.contentView addSubview:self.detailInfoBgView];
    [self.detailInfoBgView addSubview:self.detailInfo];
}

- (void)initLayout {
    // 布局子控件
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(100);
    }];
    [self.couponImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(146, 100));
    }];
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.couponImg.mas_right).offset(15);
        make.top.mas_equalTo(10);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(22);
    }];
    
    [self.useBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.size.mas_equalTo(CGSizeMake(52, 26));
    }];
    [self.date mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.title.mas_left);
        make.top.mas_equalTo(self.title.mas_bottom).offset(20);
        make.right.mas_equalTo(-10);
    }];
    [self.lineImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.title.mas_left);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(self.date.mas_bottom).offset(11);
    }];
    [self.detailInfoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.title.mas_left);
        make.top.mas_equalTo(self.lineImg.mas_bottom);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(22);
    }];
    [self.detailInfoBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(self.bgView.mas_bottom);
        make.height.mas_equalTo(50);
    }];
    [self.detailInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(11);
        make.right.mas_equalTo(-11);
        make.height.mas_equalTo(28);
    }];
}
- (void)detailInfoTapped:(UIButton *)button {
    button.selected = !button.selected;
    if (button.selected) {
        self.detailInfoBgView.hidden = NO;
    }else {
        self.detailInfoBgView.hidden = YES;
    }
    if (self.useDetailBlock) {
        self.useDetailBlock(button.selected);
    }
}
- (void)configCellWithModel:(DiscountCoupons *)model discountCouponsType:(DiscountCouponsType)discountCouponsType{
    
    //隐藏使用详情
    self.detailInfoBgView.hidden = YES;
    [self.couponImg sd_setImageWithURL:[NSURL URLWithString:model.imgName] placeholderImage:[UIImage imageNamed:@"placeholder_large"]];
    self.detailInfo.text = @"豚券可用于抵减购买商品、领养小猪、小猪阶段性费用；券可叠加使 在同一订单内，不设找零";
    switch (discountCouponsType) {
        case DiscountCouponsTypeUnUse:
            self.date.text = [NSString stringWithFormat:@"%@至%@",model.validStartTime,model.validEndTime];
            [self.useBtn setBackgroundImage:[UIImage imageNamed:@"discountcoupon_touse"] forState:UIControlStateNormal];
            break;
        case DiscountCouponsTypeUsed:
            self.date.text = [NSString stringWithFormat:@"%@至%@",model.validStartTime,model.validEndTime];
            self.useBtn.selected = NO;
            [self.useBtn setTitle:@"已使用" forState:UIControlStateNormal];
            break;
        case DiscountCouponsTypeExpired:
            self.date.text = [NSString stringWithFormat:@"%@至%@",model.validStartTime,model.validEndTime];
            self.useBtn.selected = NO;
            [self.useBtn setTitle:@"已过期" forState:UIControlStateNormal];
            
            break;
        case DiscountCouponsTypeSelect:
        {
            self.date.text = [NSString stringWithFormat:@"有效期:%@至%@",model.validStartTime,model.validEndTime];
            self.detailInfoBgView.hidden = YES;
            self.detailInfoBtn.hidden = YES;
            self.lineImg.hidden = YES;
            [self.date mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.title.mas_bottom).offset(34);
            }];
            [self.useBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(41);
                make.right.mas_equalTo(-15);
                make.size.mas_equalTo(CGSizeMake(18, 18));
            }];
            if (model.isSelect) {
                [self.useBtn setBackgroundImage:[UIImage imageNamed:@"adopt_buy_selected"] forState:UIControlStateNormal];

            } else {
                [self.useBtn setBackgroundImage:[UIImage imageNamed:@"meat_cart_unchecked"] forState:UIControlStateNormal];
            }
        }
            break;
        default:
            break;
    }

}

#pragma mark - getter and setter

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.shadowRadius = 5;
        _bgView.layer.shadowOffset = CGSizeMake(0, 0);
        _bgView.layer.shadowColor = [UIColor colorWithHexString:@"#F0F2F3"].CGColor;
        _bgView.layer.shadowOpacity = 0.7;
    }
    return _bgView;
}
- (UIImageView *)couponImg {
    if (!_couponImg) {
        _couponImg = [[UIImageView alloc] init];
        _couponImg.layer.shadowRadius = 5;
    }
    return _couponImg;
}
- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.textColor = [UIColor textBlackColor];
        _title.font = [UIFont systemFontOfSize:16];
        _title.text = @"豚券";
    }
    return _title;
}
- (UILabel *)date {
    if (!_date) {
        _date = [[UILabel alloc] init];
        _date.font = [UIFont systemFontOfSize:10];
        _date.textColor = [UIColor colorWithHexString:@"#363636"];
    }
    return _date;
}
- (UIButton *)useBtn {
    if (!_useBtn) {
        _useBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_useBtn setTitleColor:[UIColor colorWithHexString:@"#BDBDBD"] forState:UIControlStateNormal];
        _useBtn.titleLabel.font = [UIFont systemFontOfSize:13];
       }
    return _useBtn;
}
- (UIButton *)detailInfoBtn {
    if (!_detailInfoBtn) {
        _detailInfoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_detailInfoBtn setTitle:@"使用信息" forState:UIControlStateNormal];
        [_detailInfoBtn setTitleColor:[UIColor colorWithHexString:@"#9C9C9C"] forState:UIControlStateNormal];
        _detailInfoBtn.titleLabel.font = [UIFont systemFontOfSize:10];
        [_detailInfoBtn setImage:[UIImage imageNamed:@"discountcoupon_up"] forState:UIControlStateNormal];
        [_detailInfoBtn setImage:[UIImage imageNamed:@"discountcoupon_down"] forState:UIControlStateSelected];
        
        [_detailInfoBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -80 * [JTool getScreenWidthScale], 0, 90* [JTool getScreenWidthScale])];
        [_detailInfoBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 90* [JTool getScreenWidthScale], 0, -90* [JTool getScreenWidthScale])];
        [_detailInfoBtn addTarget:self action:@selector(detailInfoTapped:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _detailInfoBtn;
}
- (UIImageView *)lineImg {
    if (!_lineImg) {
        _lineImg = [[UIImageView alloc] init];
        _lineImg.image = [UIImage imageNamed:@"discountcoupon_line"];
    }
    return _lineImg;
}
- (UIView *)detailInfoBgView {
    if (!_detailInfoBgView) {
        _detailInfoBgView = [[UIView alloc] init];
        _detailInfoBgView.backgroundColor = [UIColor globalBackgroundColor];
    }
    return _detailInfoBgView;
}
- (UILabel *)detailInfo {
    if (!_detailInfo) {
        _detailInfo = [[UILabel alloc] init];
        _detailInfo.textColor = [UIColor colorWithHexString:@"#838383"];
        _detailInfo.font = [UIFont systemFontOfSize:10];
        _detailInfo.numberOfLines = 0;
    }
    return _detailInfo;
}

@end
